# -*- coding: utf-8 -*-
"""
ROC曲线
Created on Wed Apr 25 10:26:15 2018

@author: Allen
"""
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets

digits = datasets.load_digits()
X = digits.data
y = digits.target.copy()

y[digits.target == 9] = 1
y[digits.target != 9] = 0

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, random_state = 666 )

from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression()
log_reg.fit( X_train, y_train )
decision_scores = log_reg.decision_function( X_test )

thresholds = np.arange( np.min( decision_scores ), np.max( decision_scores ), 0.1 )
tprs = []
fprs = []

from playML.metrics import TPR
from playML.metrics import FPR
for threshold in thresholds:
    y_predict = np.array( decision_scores >= threshold, dtype = "int" )
    tprs.append( TPR( y_test, y_predict ) )
    fprs.append( FPR( y_test, y_predict ) )
 
plt.plot( fprs, tprs )
plt.show()

### sklearn 中的 ROC 曲线
from sklearn.metrics import roc_curve
fprs, tprs, thresholds = roc_curve( y_test, decision_scores )

plt.plot( fprs, tprs )
plt.show()
'''
曲线下面积越大，说明越好。这是因为fpr（犯错越少）的时候，
tpr（正确率越高）
'''
### 面积的求法
from sklearn.metrics import roc_auc_score
print( roc_auc_score( y_test, decision_scores ) )  # 0.98304526749
'''
作用：
    比较两个模型孰优孰劣
'''